package com.nlscan.qdkit.ui.publico.art.mvvm

import android.app.ProgressDialog
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.viewbinding.ViewBinding
import com.nlscan.qdkit.ui.publico.R
import com.nlscan.qdkit.ui.publico.config.PublicoConfig
import com.nlscan.qdkit.ui.publico.config.PublicoConfigHelper
import com.nlscan.qdkit.ui.publico.util.SnackBarUtil
import com.quvii.qvlib.util.ClickFilter
import com.quvii.qvlib.util.QvSystemUtil
import com.quvii.qvmvvm.core.base.activity.KtxBaseVMActivity
import com.wuhenzhizao.titlebar.widget.CommonTitleBar

abstract class BaseMvvmActivity<T : ViewBinding> : KtxBaseVMActivity<T>() {
    protected val mTitleBar: CommonTitleBar by lazy { binding.root.findViewById(R.id.publico_title_bar) }
    protected var mProgressDialog: ProgressDialog? = null


    override fun onPreStart(): Boolean {
        //初始化Activity的方向
        PublicoConfigHelper.configDefaultActivityOrientation(this@BaseMvvmActivity)
        return super.onPreStart()
    }

    // TODO: think
    override fun getStatusBarColor(): Int {
        return R.color.colorPrimary
    }

    override fun showOrHideLoading(isShow: Boolean) {
        if (isShow) {
            initProgressDialog()
            mProgressDialog?.apply {
                if (!isShowing) {
                    setOnCancelListener { finish() }
                    show()
                    setContentView(R.layout.publico_custom_progress_dlg)
                }
            }
        } else {
            mProgressDialog?.apply {
                if (isShowing) {
                    dismiss()
                }
            }
        }
    }

    protected open fun initProgressDialog() {
        if (null == mProgressDialog) {
            val progressDialog = ProgressDialog(mContext, R.style.CustomDialog)
            progressDialog.setCanceledOnTouchOutside(false)
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER)
            mProgressDialog = progressDialog
        }
    }

    override fun showResult(code: Int) {
    }

    protected open fun setTransparentTitleBar(isFinish: Boolean) {
        setTitleBar("", R.color.transparent, isFinish)
    }

    protected open fun setTitleBar(title: String?) {
        setTitleBar(title, R.color.publico_title_bar_bg, true)
    }

    protected open fun setTitleBar(title: String?, colorId: Int) {
        setTitleBar(title, colorId, true)
    }

    protected open fun setTitleBar(title: String?, isFinish: Boolean) {
        setTitleBar(title, R.color.publico_title_bar_bg, isFinish)
    }

    /**
     * 设置标题栏
     *
     * @param title    标题
     * @param bgColorId  标题栏背景色
     * @param isFinish true表示左侧显示返回按钮
     */
    protected open fun setTitleBar(title: String?, bgColorId: Int, isFinish: Boolean) {
        setTitleName(title)
        setTitleBarBg(bgColorId)
        if (isFinish) {
            val btnLeft: ImageButton = mTitleBar.leftImageButton
            /// 全局背景白色底色适配
            val resId: Int = if (PublicoConfig.IS_WHITE_BG && bgColorId == R.color.transparent) {
                R.drawable.publico_selector_btn_back_black
            } else {
                R.drawable.publico_selector_btn_back
            }
            btnLeft.setImageResource(resId)
            mTitleBar.setListener { _: View?, action: Int, extra: String? ->
                if (action == CommonTitleBar.ACTION_LEFT_BUTTON) {
                    finish()
                }
            }
        } else {
            mTitleBar.leftImageButton.visibility = View.INVISIBLE
        }
    }

    protected open fun setTitleBar(title: String?, resId: Int, listener: View.OnClickListener?) {
        setTitleBar(title, R.color.publico_title_bar_bg, resId, listener)
    }

    protected open fun setTitleBar(
        title: String?,
        colorId: Int,
        resId: Int,
        listener: View.OnClickListener?
    ) {
        setTitleName(title)
        setTitleBarBg(colorId)
        val btnLeft: ImageButton = mTitleBar.leftImageButton
        btnLeft.setImageResource(resId)
        btnLeft.setOnClickListener(listener)
    }


    open fun getTitleName(): String {
        return mTitleBar.centerTextView.text.toString().trim()
    }

    /**
     * 设置标题
     */
    open fun setTitleName(title: String?) {
        if (!TextUtils.isEmpty(title)) {
            val tvCenter: TextView = mTitleBar.centerTextView
            tvCenter.visibility = View.VISIBLE
            tvCenter.text = title
        }
    }

    open fun setTitleNameColor(colorId: Int) {
        val tvCenter: TextView = mTitleBar.centerTextView
        tvCenter.setTextColor(resources.getColor(colorId))
    }

    /**
     * 设置标题栏背景色
     * -- 1.适配标题文字颜色
     * -- 2019-1-17  2. 适配沉浸式状态栏图标颜色模式
     */
    fun setTitleBarBg(@ColorRes bgColorId: Int) {
        val bgColor = resources.getColor(bgColorId)
        mTitleBar.setBackgroundColor(bgColor)
        //TODO fillStatusBar 未暴露出来，当前默认支持沉浸式
        mTitleBar.setStatusBarColor(bgColor) //状态栏背景色与标题栏背景色保持一致
        //如果标题栏背景色为白色，修改字体颜色为黑色，若为其他颜色，标题文字改为白色
        val tvCenter = mTitleBar.centerTextView
        if (QvSystemUtil.isLightColor(resources.getColor(bgColorId))) {
            tvCenter.setTextColor(Color.parseColor("#333333"))
            // 2. 适配沉浸式状态栏图标颜色模式
            mTitleBar.setStatusBarMode(CommonTitleBar.STATUS_BAR_MODE_DARK)
        } else {
            tvCenter.setTextColor(Color.WHITE)
            mTitleBar.setStatusBarMode(CommonTitleBar.STATUS_BAR_MODE_LIGHT)
        }
    }


    /**
     * CommonTitleBar 设置默认leftType，调用此函数需要将
     * leftType 设置为 ImageButton
     *
     * @param resId
     * @param listener
     */
    fun setTitleBarLeftBtn(@DrawableRes resId: Int, listener: View.OnClickListener?) {
        // [bugFix]空指针异常
        if (mTitleBar == null) return
        val btnLeft = mTitleBar!!.leftImageButton ?: return
        btnLeft.setImageResource(resId)
        btnLeft.setOnClickListener(listener)
    }


    /**
     * 设置右侧为返回主页面按钮
     */
//    open fun setRightBackBtn() {
//        setRightBtn(R.drawable.publico_selector_btn_cancel) {
//            backToMain()
//        }
//    }

    open fun setTitleBarRightBtn(resId: Int, listener: View.OnClickListener?) {
        val btnRight: ImageButton = mTitleBar.rightImageButton ?: return
        btnRight.setImageResource(resId)
        btnRight.setOnClickListener(listener)
    }

    /**
     * 设置左侧按钮显隐状态
     */
    fun setLeftImageButtonVisible(isShow: Boolean) {
        val btnLeft = mTitleBar!!.leftImageButton ?: return
        btnLeft.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    /**
     * 设置右侧按钮显隐状态
     */
    fun setRightImageButtonVisible(isShow: Boolean) {
        val btnRight = mTitleBar!!.rightImageButton ?: return
        btnRight.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    fun showSnackBar(res: Int) {
        SnackBarUtil.Show(mTitleBar, getString(res))
    }

    fun showSnackBar(info: String?) {
        SnackBarUtil.Show(mTitleBar, info)
    }


    // TODO: 2021/3/30 可迁移到底层库

    fun setClickEvent(
        vararg views: View,
        needFilterClick: Boolean = true,
        listener: (view: View) -> Unit
    ) {
        for (view in views) {
            view.setOnClickListener {
                //过滤反复点击
                if (needFilterClick && ClickFilter.filter(it.id)) {
                    return@setOnClickListener
                }

                listener(view)
            }
        }
    }

//    protected open fun backToMain() {
//        RouterCenter.navigationActivity(this, Router.Main.A_MAIN)
//        finish()
//    }
}